﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
using StockholmsStad.Web.UI.Class.Interfaces;
using StockholmsStad.Web.UI.UIServiceReference;
using StockholmsStad.Web.UI.Class.Utilities;

namespace StockholmsStad.Web.UI.Registrering
{
    /// Andrasida för registreringen. Innehåller
    /// kontaktinformation och ceremoniinformation
    public partial class Registration2_Contact : UserControl, IRegistration2_Contact
    {
        /// <summary>
        /// Ceremony måste mellanlagras eftersom Page_Load inte är körd ännu;
        /// </summary>
        private Ceremony _ceremony;

        /// <summary>
        /// MVC Controller
        /// </summary>
        public IRegistrationInformationController Controller { get; set; }

        /// <summary>
        /// Parets kontaktuppgifter eller kontaktperson
        /// </summary>
        public ContactPerson Contact
        {
            get
            {
                return new ContactPerson
                           {
                               Email = txtContactEmail.Text,
                               FirstNames = txtContactFirstName.Text,
                               LastName = txtContactLastName.Text,
                               Telephone = txtContactPhone.Text,
                           };
            }
            set
            {
                txtContactEmail.Text = value.Email;
                txtContactFirstName.Text = value.FirstNames;
                txtContactLastName.Text = value.LastName;
                txtContactPhone.Text = value.Telephone;
            }
        }

        /// <summary>
        /// Parets ceremoniinformation
        /// </summary>
        public Ceremony CeremonyInformation
        {
            get
            {
                return new Ceremony
                           {
                               CeremonyLanguage = new ValueIdPair
                                                      {
                                                          Name = rblCeremonyLanguage.SelectedItem.Text,
                                                          Id = Convert.ToInt32(rblCeremonyLanguage.SelectedItem.Value)
                                                      },
                               CertificateLanguage = new ValueIdPair
                                                         {
                                                             Name = ddlCertificateLanguage.SelectedItem.Text,
                                                             Id =
                                                                 Convert.ToInt32(
                                                                 ddlCertificateLanguage.SelectedItem.Value)
                                                         },
                               SpecialNeeds =
                                   ddlSpecialNeeds.SelectedItem.Text.Equals("--")
                                       ? new ValueIdPair[0]
                                       : new ValueIdPair[]
                                             {
                                                 new ValueIdPair
                                                     {
                                                         Name = ddlSpecialNeeds.SelectedItem.Text,
                                                         Id = Convert.ToInt32(ddlSpecialNeeds.SelectedItem.Value)
                                                     }
                                             }
                           };
            }
            set { SetCeremony(value); }
        }

        #region IRegistration2_Contact Members

        /// <summary>
        /// Uppdatera namnet på person1 i listan över valbara kontaktpersoner
        /// </summary>
        /// <param name="name"></param>
        public void UpdateContactPersonListName(string name)
        {
            rblContactPerson.Items[0].Text = name;
        }

        /// <summary>
        /// Sätter namnet på kontaktpersonen
        /// </summary>
        /// <param name="firstName"></param>
        /// <param name="lastName"></param>
        public void UpdateContactPersonName(string firstName, string lastName)
        {
            txtContactFirstName.Text = firstName;
            txtContactLastName.Text = lastName;
        }

        /// <summary>
        /// Sätter telefon och email på kontaktpersonen
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="email"></param>
        public void UpdateContactPersonDetails(string phone, string email)
        {
            txtContactPhone.Text = phone;
            txtContactEmail.Text = email;
        }

        /// <summary>
        /// Välj om kontaktsnamnsfältet ska vara skrivbart
        /// </summary>
        /// <param name="enabled"></param>
        public void UpdateContactPersonName(bool enabled)
        {
            txtContactFirstName.TextEnabled = enabled;
            txtContactLastName.TextEnabled = enabled;
        }

        /// <summary>
        /// Svarar om den valda kontaktpersonen är person1
        /// </summary>
        /// <returns></returns>
        public bool ContactPersonListIsFirstPerson()
        {
            return rblContactPerson.SelectedValue.Equals("person1");
        }

        /// <summary>
        /// Uppdatera kontaktperson och ceremoniinformation
        /// </summary>
        /// <param name="contact"></param>
        /// <param name="ceremony"></param>
        /// <param name="contactIsFirstPerson"></param>
        public void UpdateContactAndCeremony(ContactPerson contact, Ceremony ceremony, bool contactIsFirstPerson)
        {
            CeremonyInformation = ceremony;
            Contact = contact;
            rblContactPerson.SelectedValue = contactIsFirstPerson ? "person1" : "annan";
        }

        #endregion

        private void SetCeremony(Ceremony value)
        {
            _ceremony = value;
            if (rblCeremonyLanguage.Items.Count != 0)
            {
                rblCeremonyLanguage.SelectedValue = CollectionUtil.GetValue(rblCeremonyLanguage.Items, _ceremony.CeremonyLanguage.Name);
                ddlCertificateLanguage.SelectedValue = CollectionUtil.GetValue(ddlCertificateLanguage.Items, _ceremony.CertificateLanguage.Name);
                if (_ceremony.SpecialNeeds.Length != 0)
                    ddlSpecialNeeds.SelectedValue = CollectionUtil.GetValue(ddlSpecialNeeds.Items, _ceremony.SpecialNeeds[0].Name);
            }
        }

        /// <summary>
        /// Körs när sidan laddas
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                Initialize();
            }
            if (_ceremony != null)
            {
                rblCeremonyLanguage.SelectedValue = _ceremony.CeremonyLanguage.Name;
                ddlCertificateLanguage.SelectedValue = _ceremony.CertificateLanguage.Name;
                if (_ceremony.SpecialNeeds.Length != 0)
                    ddlSpecialNeeds.SelectedValue = _ceremony.SpecialNeeds[0].Name;
            }
        }

        private void Initialize()
        {
            IEnumerable<ValueIdPair> languages = Controller.GetCeremonyLanguages().Where(l => l.Id == 1 || l.Id == 2 ); //Only swedish and english;
            rblCeremonyLanguage.DataValueField = "Id";
            rblCeremonyLanguage.DataTextField = "Name";
            rblCeremonyLanguage.DataSource = languages;
            rblCeremonyLanguage.DataBind();
            var defaultSelected = languages.Where(l => l.Id == 1).FirstOrDefault();
            rblCeremonyLanguage.SelectedValue = rblCeremonyLanguage.Items.FindByValue(defaultSelected.Id.ToString()).Value;

            IEnumerable<ValueIdPair> specialNeeds = Controller.GetCeremonySpecialNeeds();
            IList<ValueIdPair> needs = new List<ValueIdPair>(specialNeeds);
            needs.Insert(0, new ValueIdPair{Name = "--"});
            ddlSpecialNeeds.DataValueField = "Id";
            ddlSpecialNeeds.DataTextField = "Name";
            ddlSpecialNeeds.DataSource = needs;
            ddlSpecialNeeds.DataBind();

            IEnumerable<ValueIdPair> languages1 = Controller.GetWeddingCertificateLanguages();
            ddlCertificateLanguage.DataValueField = "Id";
            ddlCertificateLanguage.DataTextField = "Name";
            ddlCertificateLanguage.DataSource = languages1;
            ddlCertificateLanguage.DataBind();
            ddlCertificateLanguage.SelectedValue = "Svenska";
        }

        /// <summary>
        /// Användaren har valt antingen person1 eller annan kontaktperson
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Controller.UpdateContactName(false);
        }

        /// <summary>
        /// Kontrollerar att emailadressen är unik.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="args"></param>
        protected void UserNameCustomValidator_ServerValidate(object source, ServerValidateEventArgs args)
        {
            args.IsValid = Controller.IsUserNameUnique(txtContactEmail.Text);
        }
    }
}